//
//  SGSLoginViewController.m
//  AgileNYC
//
//  Created by German on 31.07.14.
//  Copyright (c) 2014 Softlegion. All rights reserved.
//

#import "SGSLoginViewController.h"
#import "SGSModelsManager.h"
#import "SGSSignInWithEmailViewController.h"
#import "SGSUtils.h"
#import "SGSServerManagerConsts.h"
#import "SGSEventsViewController.h"
#import "SGSDelegatesManager.h"

@interface SGSLoginViewController ()

@property (assign, nonatomic) BOOL isLoginMake;

@property (strong, nonatomic) UIActivityIndicatorView * spinner;

@end

@implementation SGSLoginViewController

- (NSUInteger)supportedInterfaceOrientations {
    return  UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {
    return YES;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.isLoginMake = NO;
    
    [self detectOrientation];

}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [self detectOrientation];
}

#pragma mark - Actions

- (IBAction)onEmailButtonClick:(id)sender {
    //SGSSignInWithEmailViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"SGSSignInWithEmailViewController"];
    
    //[self.navigationController pushViewController:vc animated:YES];
    
    SGSDelegatesManager * delegatesManager = [SGSDelegatesManager sharedDelegatesManager];
    
    [self.navigationController pushViewController:delegatesManager.currentSigninWithEmailVC animated:YES];

}

- (IBAction)onFacebookButtonClick:(id)sender {
    GSUser * loginUser = [SGSModelsManager sharedModelsManager].currentUser;
    SGSModelsManager * modelsManager = [SGSModelsManager sharedModelsManager];
    
    [self startSpinner];
    
    [modelsManager makeLoginWithFacebook:loginUser onComplete:^(BOOL result) {
        GSLog(GSLogInfo, @"Facebook login: %@", [NSNumber numberWithBool:result]);
        
        if (result) {
            [self loginWithSocial:GSFacebookLoginType];
        }
    }];
    
}

- (IBAction)onGooglePlusButtonClick:(id)sender {
    GSUser * loginUser = [SGSModelsManager sharedModelsManager].currentUser;
    SGSModelsManager * modelsManager = [SGSModelsManager sharedModelsManager];
    
    [modelsManager makeLoginWithGooglePlus:loginUser onComplete:^(BOOL result) {
        GSLog(GSLogInfo, @"GooglePlus login: %@", [NSNumber numberWithBool:result]);
        
        if (result) {
            [self loginWithSocial:GSGooglePlusLoginType];
        }
    }];
    
}


- (IBAction)onLinkedInButtonClick:(id)sender {
    GSUser * loginUser = [SGSModelsManager sharedModelsManager].currentUser;
    SGSModelsManager * modelsManager = [SGSModelsManager sharedModelsManager];
    
    [modelsManager makeLoginWithLinkedIn:loginUser onComplete:^(BOOL result) {
        GSLog(GSLogInfo, @"LinkedIn login: %@", [NSNumber numberWithBool:result]);
        
        if (result) {
            [self loginWithSocial:GSLinkedInLoginType];
        }
    }];
}

- (IBAction)onBackButtonClick:(id)sender {
    /*SGSModelsManager * modelsManager = [SGSModelsManager sharedModelsManager];
    modelsManager.controllerToBeOpened = nil;*/
    
    [SGSDelegatesManager sharedDelegatesManager].controllerToBeOpened = nil;
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Internal Functions


-(void) loginWithSocial:(NSInteger) socialType {
    GSUser * loginUser = [SGSModelsManager sharedModelsManager].currentUser;
    SGSModelsManager * modelsManager = [SGSModelsManager sharedModelsManager];
    
    [modelsManager loginUserWithSocial:loginUser.eMail socialType:socialType
        onComplete:^(BOOL result, NSError *error) {
            loginUser.isRegistered = [NSNumber numberWithBool:result];
            loginUser.isSynchronized = [NSNumber numberWithBool:YES];
            
            SGSDelegatesManager * delegatesManager = [SGSDelegatesManager sharedDelegatesManager];
            
            [self stopSpinner];
            
            delegatesManager.currentProfileVC.isNewUser = !result;
            delegatesManager.controllerToBeOpened = delegatesManager.currentProfileVC;
            [self.navigationController popToViewController:delegatesManager.currentEventsVC animated:YES];
            
        }];
}

#pragma mark - System Functions

- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}

#pragma mark - Interface Functions

-(void) detectOrientation {
    if (UIDeviceOrientationIsLandscape(self.interfaceOrientation))
    {
        //CGFloat positionX = self.view.frame.size.width - self.imgAgile.frame.size.width - 2.0;
        //CGFloat positionY = (self.view.frame.size.height - self.imgAgile.frame.size.height)/2;
        
        //self.imgAgile.frame = CGRectMake(positionX,
        //                                 positionY,
        //                                 self.imgAgile.frame.size.width,
        //                                 self.imgAgile.frame.size.height);
        
        self.imgAgile.hidden = YES;
    } else {
        self.imgAgile.hidden = NO;
    }
}

#pragma mark - Spinner

-(void) startSpinner {
    
    self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.spinner.hidesWhenStopped = YES;
    self.spinner.center = self.view.center;
   [self.view addSubview:self.spinner];
   [self.spinner startAnimating];
        
}

-(void) stopSpinner {
    
   [self.spinner stopAnimating];
   [self.spinner removeFromSuperview];
    self.spinner = nil;
}


@end
